package listing;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.testng.*;
import org.testng.annotations.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Cart extends ru.st.selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

    @Test
  public void testUntitled13() throws Exception {
    driver.get(baseUrl + "furniture.html");
    try {
      assertEquals("You have no items in your shopping cart.", driver.findElement(By.cssSelector("div.block.block-cart > div.block-content > p.empty")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.cssSelector("button.button.btn-cart")).click();
    try {
      assertEquals("Ottoman was added to your shopping cart.", driver.findElement(By.cssSelector("li > span")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.get(baseUrl + "furniture.html");
    try {
      assertEquals("Ottoman", driver.findElement(By.cssSelector("p.product-name > a")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    try {
      assertEquals("There is 1 item in your cart.", driver.findElement(By.cssSelector("div.summary > p.amount")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.cssSelector("div.block.block-cart > div.block-content > div.actions > button.button")).click();
    try {
      assertEquals("Checkout", driver.findElement(By.cssSelector("h1")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
    driver.findElement(By.linkText("Furniture")).click();
    driver.findElement(By.linkText("Remove This Item")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you would like to remove this item from the shopping cart[\\s\\S]$"));
    try {
      assertEquals("You have no items in your shopping cart.", driver.findElement(By.cssSelector("div.block.block-cart > div.block-content > p.empty")).getText());
    } catch (Error e) {
      verificationErrors.append(e.toString());
    }
  }

   private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  
  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}
